package com.ja.openchat.viewmodels;

import android.app.Application;

import androidx.annotation.NonNull;

import com.ja.openchat.ui.chat.group.ModifyFieldActivity;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgEvent;
import com.zy.openchat.core.model.message.MessageInfo;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.base.BaseViewModel;


public class ModifyFiledViewModel extends BaseViewModel {


    private ModifyFieldActivity modifyFieldActivity;

    public ModifyFiledViewModel(@NonNull Application application, BaseModel mRepository) {
        super(application);
    }



    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTgEvent(final TgEvent tg_event) {
        switch (tg_event.event) {
            case EUser_Td_Chat_Send_Message_Success:
            case EUser_Td_Chat_Send_Message_Fail: {
                MessageInfo msg = (MessageInfo) tg_event.data;
                long oldMsgId = (long) tg_event.dataEx;
                if (msg != null && modifyFieldActivity.sendingMsg != null && modifyFieldActivity.sendingMsg.id == oldMsgId) {
                    if (msg.sendState() == TelegramManager.MessageSendState.MessageSendState_Success) {
                        modifyFieldActivity.setGroupPinnedMessage_step2(msg);
                    } else {
                        showErrorTip("群公告设置失败，请稍后重试");
                    }
                }
            }
            break;
        }
    }

    public void setContext(ModifyFieldActivity modifyFieldActivity) {
        this.modifyFieldActivity = modifyFieldActivity;
    }
}
